home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
portablc.arc
/
SPLIT_F.C
< prev
next >
Wrap
Text File
|
1990-08-30
|
11KB
|
330 lines
/**********************************************************************
*
* split_f: Program to split and recombine binary files
*
* USAGE:
* split_f S Ni Complete_file Section_file_prefix
* or split_f S Ki Complete_file Section_file_prefix
* or split_f C Complete_file Section_file_list
*
* where
* S indicates "split" (may be upper or lower case)
* C indicates "combine" (may be upper or lower case)
* Ni means "split into i files of equal size"
* Ki means "split into files of size i Kilobytes"
* Complete_file is the file to be split or recombined
* Section_files are the component files after split or
* to be recombined into the Complete_file
* Section_file_prefix is the 8-character filename portion
* of the Section_files. The extension will be
* numbers generated by the program.
* Section_file_list is a sequence of Section_files, each
* separated by a space.
*
***********************************************************************
*
* REVISION HISTORY:
*
* July 7, 1986: Created (Charlie Beerman)
* Jan. 18, 1990: Updated to run in UNIX (Martin Katz)
* Note: chmod 04755 on the executable.
*
**********************************************************************/
#include <stdio.h>
#include <ctype.h>
#define FBUFSIZ 1024
#define MAXFILES 99
#define ERR (-1)
FILE *infile, *outfile;
main(argc,argv)
int argc;
char *argv[];
{
/****** start of executable code ******/
/* Check for at least 1 argument */
if (argc < 2)
userr("",0,0); /* Print usage & exit */
/* Read first argument to decide action */
switch (tolower(argv[1][0]))
{
case 'c': /* Combine */
if (argc < 4)
userr("Too few arguments",0,0);
else
combine(argc,argv);
break;
case 's': /* Split */
if (argc < 5)
userr("Too few arguments",0,0);
else
split(argc,argv);
break;
default:
userr("Invalid first argument",0,0);
break; /* Code should never get to this line */
}
} /* end of main program */
/**********************************************************************
*
* USERR: Print error message and/or help info
*
* INPUTS:
* errmsg Text to output (if of nonzero length)
* file1 File to close (if nonzero)
* file2 File to close (if nonzero)
*
**********************************************************************/
userr(errmsg,file1,file2)
char *errmsg;
int file1, file2;
{
int status;
/****** start of executable code ******/
/* close files if necessary */
if (file1 > 0)
fclose(file1);
if (file2 > 0)
fclose(file2);
/* print error message if there is one */
if (strlen(errmsg) > 0)
printf("%s\n",errmsg);
else
printf("%s%s%s%s",
"Usage:\n",
" split_f C create_file_name section_file_list\n",
" or split_f S Ni source_file_name section_file_prefix\n",
" or split_f S Ki source_file_name section_file_prefix\n");
/* now exit to DOS with errorlevel 1 */
exit(1);
} /* end USERR */
/**********************************************************************
*
* COMBINE: Combine a list of files into one file
*
* INPUTS:
* argc Count of command-line arguments
* argv List of command-line arguments
*
* OUTPUTS:
* none
*
**********************************************************************/
combine(argc,argv)
int argc;
char *argv[];
{
int i;
int nread, nwrote;
long destsiz;
char buf[FBUFSIZ];
/****** start of executable code ******/
/* open file to recombine */
printf("Creating file %s\n",argv[2]);
if ((outfile = fopen( argv[2], "w+b" )) == NULL )
userr("Error in opening output file",0,0);
destsiz = 0l;
/* loop through section files */
for (i = 3; i < argc; ++i)
{
/* open source file */
printf("Reading from file %s\n",argv[i]);
if ((infile = fopen( argv[i], "r+b" )) == NULL )
userr("Error in opening input file",outfile,0);
/* loop through source file, reading a buffer at a time
into the destination file */
while ( (nread = fread(buf,1,FBUFSIZ,infile)) != 0 )
{
nwrote = fwrite (buf, 1, nread, outfile);
destsiz += nwrote;
}
/* now close source file */
fclose(infile);
} /* end of loop over files in argument list */
/* now close destination file */
fclose(outfile);
printf("Total bytes written to %s: %ld.\n",argv[2], destsiz);
} /* end of COMBINE */
/**********************************************************************
*
* SPLIT: Split a file into components
*
* INPUTS:
* argc count of arguments on command line
* argv text of arguments on command line
*
**********************************************************************/
split(argc,argv)
int argc;
char *argv[];
{
int i, nfiles;
long maxsiz, srcsiz, destsiz, pos;
int nread, nwrote, nbytes;
char buf[FBUFSIZ], *p;
char prefix[61], secname[65], numstr[3];
/****** start of executable code ******/
/* figure out number of files to write and their maximum size */
p = argv[2];
switch (tolower(*p))
{
case 'n': /* user entered number of files */
maxsiz = -1L;
if ((nfiles = atoi(++p)) <= 0 ||
nfiles > MAXFILES)
userr("Invalid number of files",0,0);
break;
case 'k': /* user entered maximum filesize */
nfiles = -1;
maxsiz = ((long) atoi(++p)) * 1024L;
if (maxsiz < (long) (FBUFSIZ))
userr("Maximum size too small",0,0);
break;
default:
userr("Invalid second argument",0,0);
break;
} /* end of switch */
/* open source file and find its size */
if ((infile = fopen( argv[3], "r+b" )) == NULL )
userr("Error in opening source file",0,0);
else
fseek(infile,0L,2);
if ((srcsiz = ftell(infile)) <= (long) (FBUFSIZ))
{
printf("Source size is %ld.\n", srcsiz);
userr("Source file smaller than minimum split",infile,0);
}
else
{
printf("Splitting file %s of size %ld\n",argv[3],srcsiz);
rewind(infile); /* rewind file */
}
/* finish doing filesize / # files calculation */
if (nfiles == -1)
{
nfiles = (int) (srcsiz / maxsiz +1);
if (nfiles > MAXFILES)
userr("Too many section files",infile,0);
}
else if (maxsiz == -1L)
{
maxsiz = srcsiz / nfiles + 1;
if (maxsiz < (long) (FBUFSIZ))
userr("Section file size too small",infile,0);
}
printf("Number of section files: %2d\n",nfiles);
printf("Maximum section file size: %ld\n",maxsiz);
/* get section file name prefix and add ".-" identifier */
if (strlen(argv[4]) <= 0 || strlen(argv[4]) > 60)
userr("Section file prefix too long",infile,0);
else
{
strcpy(prefix,argv[4]);
strcat(prefix,".-");
}
/* now loop over the number of files desired */
pos = 0L;
for (i = 1; i <= nfiles; ++i)
{
/* create filename */
strcpy(secname,prefix);
nwrote = sprintf(numstr,"%02d",i);
strcat(secname,numstr);
/* initialize for this file */
destsiz = 0L;
/* open file */
printf("Creating section file %s: ",secname);
if ((outfile = fopen( secname, "w+b" )) == NULL )
userr("Error in opening section file",infile,0);
/* read buffers from source file into section file
until maximum section file size is reached */
nbytes = FBUFSIZ;
nread = FBUFSIZ;
while (nread == FBUFSIZ)
{
/* are we within a bufferful of the maximum filesize? */
if ((destsiz + (long) nbytes) > maxsiz)
nbytes = (int) (maxsiz - destsiz);
/* read from source */
if (nbytes > 0)
nread = fread(buf,1,nbytes,infile);
else
nread = 0;
if (nread == ERR)
userr("Error in reading source file",
infile,outfile);
/* write to destination */
if (nread > 0)
{
nwrote = fwrite (buf, 1, nread, outfile);
destsiz += nwrote;
}
else
nwrote = 0;
if (nwrote == ERR)
userr("Error in writing section file",
infile,outfile);
} /* end while */
/* close this destination file */
pos += destsiz;
fclose(outfile);
printf(" %ld bytes\n",destsiz);
} /* end for loop */
/* close source file */
fclose(infile);
printf("Total length: %ld bytes\n",pos);
} /* end SPLIT */